package com.atguigu.guli.service.edu.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Miluo
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/admin/edu/teacher")
@Api(tags = "讲师管理")

public class AdminTeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("远程调用测试")
    @GetMapping("/test")
    public R test1(){
        ossFileService.test();
        return R.ok();
    }


    @ApiOperation("根据左关键字查询名称列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(@ApiParam(value = "关键字",required = true) @PathVariable String key){
        List<Map<String, Object>> nameList = teacherService.selectNameListByKey(key);

        return R.ok().data("nameList", nameList);
    }




    @ApiOperation("讲师批量删除")
    @DeleteMapping("/batch-remove")
    public R removeRows( @ApiParam(value = "讲师id列表", required = true) @RequestBody List<String> idList){
        boolean result = teacherService.removeByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    //查询所有教师
    @GetMapping("/list")
    @ApiOperation(value = "查询所有老师")
    public R selectTeaAll(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().message("查询成功").data("items",list);
    }

    //分页查询教师
   /* @GetMapping("/list/{pages}/{limit}")
    @ApiOperation("分页查询所有老师")
    public R selectTeaAll(@ApiParam(required = true) @PathVariable Integer pages,@ApiParam(required = true) @PathVariable Integer limit){
        Page<Teacher> page = new Page<>(pages,limit);
        Page<Teacher> pageParam = teacherService.page(page, null);
        return R.ok().data("items",pageParam).message("查询成功");
    }*/

    //分页可带条件查询教师列表
    @ApiOperation("分页讲师列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Integer page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Integer limit,
                      @ApiParam("讲师列表查询对象") TeacherQuery teacherQuery){

        IPage<Teacher> pageModel = teacherService.selectTeaByCondition(page, limit, teacherQuery);
        return  R.ok().data("pageModel", pageModel);
    }



    //根据ID查询教师信息
    @GetMapping("/select/{id}")
    @ApiOperation(value = "根据Id查询老师")
    public R selectTeacher(@ApiParam(value = "id",required = true) @PathVariable String id){
        Teacher teacher =  teacherService.getById(id);

        if (teacher != null){
            return R.ok().data("items",teacher).message("查询成功");
        }else{
            return R.error().message("查询错误");
        }
    }



    //根据Id删除老师
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据Id删除老师",notes = "根据Id删除老师")
    public R deleteTeaById(@ApiParam(required = true) @PathVariable String id){
        teacherService.removeAvatarById(id);
        boolean b = teacherService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }


    //添加教师
    @ApiOperation("添加教师")
    @PostMapping("/save")
    public R saveTea(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save){
            return R.ok().message("保存成功");
        }

        return R.error().message("保存失败");
    }

    //修改教师
    @ApiOperation("修改教师信息")
    @PutMapping("/update")
    public R updateTeaById(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b){
            return R.ok().message("修改成功");
        }
        return R.error().message("无此数据");
    }



}

